#include <iostream>
using namespace std;

#include <vector>

class Solution
{
public:
    int maxIncreaseKeepingSkyline(vector<vector<int>>& grid)
    {
        int n = grid.size(), m = grid[0].size();
        vector<int> row(n), col(m);

        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
                row[i] = max(row[i], grid[i][j]);
                col[j] = max(col[j], grid[i][j]);
            }
        }

        int ret = 0;
        for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++)
                ret += min(row[i], col[j]) - grid[i][j];

        return ret;
    }
};


int main()
{
    Solution s;
    vector<vector<int>> nums = { {3,0,8,4},{2,4,5,7},{9,2,6,3},{0,3,1,0} };
    int ret = s.maxIncreaseKeepingSkyline(nums);
    cout << ret << endl;
    return 0;
}